package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.AssessRecordBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-07-07.
 * Description:评估记录列表适配器
 */
public class AssessRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<AssessRecordBean> datas;
    private LayoutInflater mInflater;

    private itemClickListener listener;
    private ResultSelectedListener resultListener;

    public AssessRecordAdapter(Context mContext, List<AssessRecordBean> datas,
                               itemClickListener listener, ResultSelectedListener resultListener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.resultListener = resultListener;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setData(List<AssessRecordBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_reset_assessrecordlist, null);
        //AssessRecordViewholder holder = new AssessRecordViewholder(view);
        ViewHoderPush holder = new ViewHoderPush(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHoderPush viewholder = (ViewHoderPush) holder;
        AssessRecordBean bean = datas.get(position);

        viewholder.ll_assessrecordlist_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
        viewholder.tv_assessrecordlist_name.setText(bean.getName());
        viewholder.tv_assessrecord_grade.setText(bean.getGrade());
        if (bean.getIsAnswer().equals("0")) {//患者未答题
            //  viewholder.tv_answer.setVisibility(View.VISIBLE);
            viewholder.tv_assessrecordlist_doctor.setText("患者未答题");
            viewholder.tv_assessrecordlist_answer.setVisibility(View.GONE);
        } else {
            viewholder.tv_assessrecordlist_answer.setVisibility(View.VISIBLE);

            //  viewholder.tv_answer.setVisibility(View.GONE);
        }

        if (bean.isDisplay()){
            viewholder.btn_assessrecordlist_select.setVisibility(View.VISIBLE);
        }else {
            viewholder.btn_assessrecordlist_select.setVisibility(View.GONE);
        }

        if ("2".equals(bean.getAnswerType())) {
            viewholder.tv_assessrecordlist_doctor.setText("评测人:" + bean.getAnswerPerson());
        } else if (!bean.getIsAnswer().equals("0")) {//患者自评
            viewholder.tv_assessrecordlist_doctor.setText("患者自评:");
        }
        viewholder.tv_assessrecordlist_answer.setText(bean.getScore() + "分");
        if (!TextUtils.isEmpty(bean.getDate())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(Long.valueOf(bean.getDate())));
            viewholder.tv_assessrecordlist_date.setText("发送时间:" + date);
        }
        if (datas.get(position).isSelect()) {
//            viewholder.btn_select.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect_pre));
            viewholder.btn_assessrecordlist_select.setBackgroundResource(R.mipmap.btn_multiselect_pre);
        } else {
//            viewholder.btn_select.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect));
            viewholder.btn_assessrecordlist_select.setBackgroundResource(R.mipmap.btn_multiselect);
        }
        viewholder.btn_assessrecordlist_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.get(position).isSelect()) {
                    resultListener.onSelected("no_select", position);
                } else {
                    resultListener.onSelected("select", position);
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface itemClickListener {
        void onItemClick(int position);
    }

    public interface ResultSelectedListener {
        void onSelected(String type, int position);
    }

    class ViewHoderPush extends RecyclerView.ViewHolder {
        public Button btn_assessrecordlist_select;//选择框
        public LinearLayout ll_assessrecordlist_root;//点击部分
        public TextView tv_assessrecordlist_name;//标题
        public TextView tv_assessrecord_grade;//等级
        public TextView tv_assessrecordlist_doctor;//评测人
        public TextView tv_assessrecordlist_answer;//分数
        public TextView tv_assessrecordlist_date;//日期

        public ViewHoderPush(View itemView) {
            super(itemView);
            btn_assessrecordlist_select = (Button) itemView.findViewById(R.id.btn_assessrecordlist_select);
            ll_assessrecordlist_root = (LinearLayout) itemView.findViewById(R.id.ll_assessrecordlist_root);
            tv_assessrecordlist_name = (TextView) itemView.findViewById(R.id.tv_assessrecordlist_name);
            tv_assessrecord_grade = (TextView) itemView.findViewById(R.id.tv_assessrecord_grade);
            tv_assessrecordlist_doctor = (TextView) itemView.findViewById(R.id.tv_assessrecordlist_doctor);
            tv_assessrecordlist_answer = (TextView) itemView.findViewById(R.id.tv_assessrecordlist_answer);
            tv_assessrecordlist_date = (TextView) itemView.findViewById(R.id.tv_assessrecordlist_date);

        }
    }
}
